package com.suyeer.basic.util;


import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author jun 2018/10/19
 */
public class DateUtil {
    private static String dateFormatStr = "yyyy-MM-dd HH:mm:ss";
    private static String shortDateFormatStr = "yyyy-MM-dd";
    private static String monthDateFormatStr = "yyyy年MM月";
    private static String shortTimeFormatStr = "yyyyMMddHHmmssSSS";
    private static String timeFormatStr = "HH:mm:ss";

    public static String getDateStr(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(dateFormatStr);
        return df.format(date);
    }

    public static String getOnlyTimeStr(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(timeFormatStr);
        return df.format(date);
    }

    public static String getShortDateStr(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(shortDateFormatStr);
        return df.format(date);
    }

    public static String getDayStr(Date date) {
        return (new SimpleDateFormat("dd日")).format(date);
    }

    public static String getMonthStr(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(monthDateFormatStr);
        return df.format(date);
    }

    public static Date getDateFromStr(String dateStr) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(dateFormatStr);
        return df.parse(dateStr);
    }

    public static String getShortTimeFormStr(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(shortTimeFormatStr);
        return df.format(date);
    }

    public static Boolean isSameDay(Date date1, Date date2) {
        return calcDay(date1, date2).equals(0);
    }

    public static Boolean checkExpired(Date date, int expiredSecond) {
        Date expiredDate = DateUtils.addSeconds(date, expiredSecond);
        return new Date().after(expiredDate);
    }

    public static Long getTimestampLong() {
        return System.currentTimeMillis() / 1000;
    }

    public static Long getTimestampLong(Date date) {
        return date.getTime() / 1000;
    }

    public static String getTimestampString() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }

    public static String getTimestampString(Date date) {
        return Long.toString(date.getTime() / 1000);
    }

    /**
     * 获取今天最后一秒的时间
     *
     * @return Date
     */
    public static Date getDayWithLastSecond() {
        return getDayWithLastSecond(new Date());
    }

    /**
     * 获取某天最后一秒的时间
     *
     * @param date Date
     * @return Date
     */
    public static Date getDayWithLastSecond(Date date) {
        return DateUtils.addSeconds(DateUtils.truncate(DateUtils.addDays(date, 1), Calendar.DATE), -1);
    }

    /**
     * 计算天数差
     *
     * @param date Date
     * @return Integer
     */
    public static Integer calcDay(Date date) {
        return calcDay(date, new Date());
    }

    /**
     * 计算天数差
     *
     * @param date                  date
     * @param ifReturnAbsoluteValue boolean
     * @return Integer
     */
    public static Integer calcDay(Date date, boolean ifReturnAbsoluteValue) {
        return calcDay(date, new Date(), ifReturnAbsoluteValue);
    }

    /**
     * 计算天数差
     *
     * @param date1 Date
     * @param date2 Date
     * @return Integer
     */
    public static Integer calcDay(Date date1, Date date2) {
        return calcDay(date1, date2, true);
    }

    /**
     * 计算天数差
     *
     * @param date1                 Date
     * @param date2                 Date
     * @param ifReturnAbsoluteValue boolean
     * @return Integer
     */
    public static Integer calcDay(Date date1, Date date2, boolean ifReturnAbsoluteValue) {
        Date tmp1 = DateUtils.truncate(date1, Calendar.DATE);
        Date tmp2 = DateUtils.truncate(date2, Calendar.DATE);
        Long ret = (tmp1.getTime() - tmp2.getTime()) / 86400000;
        ret = ifReturnAbsoluteValue ? Math.abs(ret) : ret;
        return ret.intValue();
    }

}
